package com.hz.monitoring.module.hardware.service.mcu_tenant_type;

import com.hz.monitoring.framework.common.pojo.PageResult;
import com.hz.monitoring.module.hardware.controller.admin.mcu_tenant_type.vo.*;
import com.hz.monitoring.module.hardware.convert.mcu_tenant_ype.McuTenantTypeConvert;
import com.hz.monitoring.module.hardware.dal.dataobject.mcu_tenant_type.McuTenantTypeDO;
import com.hz.monitoring.module.hardware.dal.mysql.mcu_tenant_type.McuTenantTypeMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static com.hz.monitoring.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.hz.monitoring.module.hardware.enums.ErrorCodeConstants.MCU_TENANT_TYPE_NOT_EXISTS;

/**
 * MCU租户分类 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class McuTenantTypeServiceImpl implements McuTenantTypeService {

    @Resource
    private McuTenantTypeMapper mcuTenantTypeMapper;

    @Override
    public Long createMcuTenantType(McuTenantTypeCreateReqVO createReqVO) {
        // 插入
        McuTenantTypeDO mcuTenantType = McuTenantTypeConvert.INSTANCE.convert(createReqVO);
        mcuTenantTypeMapper.insert(mcuTenantType);
        // 返回
        return mcuTenantType.getId();
    }

    @Override
    public Boolean createBatchMcuTenantType(List<McuTenantTypeCreateReqVO> createReqVOList) {
    return mcuTenantTypeMapper.createBatchMcuTenantType(createReqVOList);
    }

    @Override
    public void updateMcuTenantType(McuTenantTypeUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateMcuTenantTypeExists(updateReqVO.getId());
        // 更新
        McuTenantTypeDO updateObj = McuTenantTypeConvert.INSTANCE.convert(updateReqVO);
        mcuTenantTypeMapper.updateById(updateObj);
    }

    @Override
    public void deleteMcuTenantType(Long id) {
        // 校验存在
        this.validateMcuTenantTypeExists(id);
        // 删除
        mcuTenantTypeMapper.deleteById(id);
    }

    private void validateMcuTenantTypeExists(Long id) {
        if (mcuTenantTypeMapper.selectById(id) == null) {
            throw exception(MCU_TENANT_TYPE_NOT_EXISTS);
        }
    }

    @Override
    public McuTenantTypeDO getMcuTenantType(Long id) {
        return mcuTenantTypeMapper.selectById(id);
    }

    @Override
    public List<McuTenantTypeDO> getMcuTenantTypeList(McuTenantTypeListReqVO listReqVO) {
        return mcuTenantTypeMapper.selectList(listReqVO);
    }

    @Override
    public PageResult<McuTenantTypeDO> getMcuTenantTypePage(McuTenantTypePageReqVO pageReqVO) {
        return mcuTenantTypeMapper.selectPage(pageReqVO);
    }

    @Override
    public List<McuTenantTypeDO> getMcuTenantTypeList(McuTenantTypeExportReqVO exportReqVO) {
        return mcuTenantTypeMapper.selectList(exportReqVO);
    }

}
